package com.ruoyi.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.service.IWsdLeavesService;
import com.ruoyi.business.service.IWsdRoleClassService;
import com.ruoyi.business.service.IWsdStudentsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdStudents;
import com.ruoyi.common.core.domain.vo.AgencyMattersVO;
import com.ruoyi.common.core.domain.vo.ChartDataVO;
import com.ruoyi.common.core.domain.vo.leaveNumberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 首页数据汇总
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("wsd_summarization")
public class WsdSummarizationController extends BaseController {

    @Autowired
    private IWsdLeavesService wsdLeavesService;

    @Autowired
    private IWsdRoleClassService wsdRoleClassService;

    @Autowired
    private IWsdStudentsService wsdStudentsService;



    /**
     * 获取周月年的柱状图
     */
    @GetMapping(value = "/LeaveStatisticsProvider")
    public AjaxResult LeaveStatisticsProvider(@RequestParam Optional<Integer> year, @RequestParam Optional<Integer> month, @RequestParam String statisticType) {
        ChartDataVO chartDataVO;
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId(); // 获取用户的学校ID
        // 根据统计类型调用不同的方法
        switch (statisticType.toUpperCase()) {
            case "YEAR":
                chartDataVO = wsdLeavesService.queryYearlyLeaveCounts(schoolId);
                break;
            case "MONTH":
                chartDataVO = wsdLeavesService.queryMonthlyLeaveCounts(schoolId,year, month);
                break;
            case "WEEK":
                chartDataVO = wsdLeavesService.queryWeeklyLeaveCounts(schoolId);
                break;
            default:
                return AjaxResult.error("无效的统计类型参数");
        }
        return AjaxResult.success(chartDataVO); // 返回封装好的Ajax结果
    }

    /**
     * 获取今天的请假情况.订餐情况
     * @return
     */

    @GetMapping(value = "/todayLeaveStatistics")
    public AjaxResult getTodayLeaveStatistics() {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        leaveNumberVO leaveNumberVO = wsdLeavesService.todayLeaveStatus(schoolId);
        return  AjaxResult.success(leaveNumberVO);
    }

    /**
     * 代办事项显示
     * @return
     */
    @GetMapping(value = "/agencyMatters")
    public AjaxResult agencyMatters() {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        AgencyMattersVO agencyMattersVO = wsdLeavesService.agencyMatters(schoolId);
        return AjaxResult.success(agencyMattersVO);
    }

    /**
     * 全班,全校人数统计
     * @return
     */
    @GetMapping(value = "/populationStatistics")
    public AjaxResult populationStatistics() {
        // Long[] classIds = wsdRoleClassService.queryList().getClassIds();
        // Long count = wsdStudentsService.populationStatistics(classIds);
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        LambdaQueryWrapper<WsdStudents> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WsdStudents::getSchoolId,schoolId);
        long count = wsdStudentsService.count(wrapper);
        return AjaxResult.success(count);
    }

}
